/**
 * 弹窗弹层
 * */
var layer_msg = window === window.top ? layer : (parent.window === parent.window.top) ? parent.layer : (parent.parent.window === parent.parent.window.top ? parent.parent.layer : parent.parent.parent.layer);

var Comm = {
    //页面加载效果
    pageLoading: function () {
        var rect = "";
        for (var i = 1; i <= 5; i++) {
            rect += '<div class="rect' + i + '"></div>&nbsp;';
        }
        return '<div class="page-loading">' +
            '<div class="layui-m-layershade" style="background-color: #fff;"></div>' +
            '<div class="spinner">' + rect +
            '</div>' +
            '</div>';
    },
    //获取url参数
    Request: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var search = window.location.search.substr(1);
        search = decodeURI(search);
        var r = search.match(reg);
        if (r != null) {
            return (r[2]);
        }
        return "";
    },
    //删除url参数值
    DeleteParas: function (url, ref) {
        var str = "";
        if (url.indexOf('?') != -1) {
            str = url.substr(url.indexOf('?') + 1);
        }
        else {
            return url;
        }
        var arr = "";
        var returnUrl = "";
        if (str.indexOf('&') != -1) {
            arr = str.split('&');
            for (var i = 0; i < arr.length; i++) {
                '';
                if (arr[i].split('=')[0] != ref) {
                    returnUrl = returnUrl + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
                }
            }
            return url.substr(0, url.indexOf('?')) + "?" + returnUrl.substr(0, returnUrl.length - 1);
        }
        else {
            arr = str.split('=');
            if (arr[0] == ref) {
                return url.substr(0, url.indexOf('?'));
            }
            else {
                return url;
            }
        }
    },
    //格式化js时间戳，格式yyyy-MM-dd HH:mm:ss
    formatDateTime: function (time) {
        if (time == "" || time == null || time == "null" || time == ".." || time == undefined) return "";
        var now = new Date(parseInt(time));
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        return Comm.fullZero(year) + "-" + Comm.fullZero(month) + "-" + Comm.fullZero(date) + " " + Comm.fullZero(hour) + ":" +
            Comm.fullZero(minute) + ":" + Comm.fullZero(second);
    },
    //格式化js时间戳，格式yyyy-MM-dd HH:mm
    formatDateTime_: function (time) {
        if (time == "" || time == null || time == "null" || time == ".." || time == undefined) return "";
        var now = new Date(parseInt(time));
        var year = now.getFullYear().toString().substring(2);
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        return year + "-" + Comm.fullZero(month) + "-" + Comm.fullZero(date) + " " + Comm.fullZero(hour)
            + ":" + Comm.fullZero(minute);
    },
    //格式化js时间戳，格式HH:dd
    dataHourMin: function (time) {
        if (time == "" || time == null || time == "null" || time == ".." || time == undefined) return "";
        var now = new Date(parseInt(time));
        var hour = now.getHours();
        var minute = now.getMinutes();
        return Comm.fullZero(hour) + ":" + Comm.fullZero(minute);
    },
    //格式化js时间戳，格式yyyy-MM-dd
    formatDate: function formatDate(time) {
        if (time == "" || time == null || time == "null" || time == ".." || time == undefined) return "";
        var now = new Date(parseInt(time));
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        return Comm.fullZero(year) + "-" + Comm.fullZero(month) + "-" + date;
    },
    //格式化js时间戳，格式yyyy-MM
    formatDateMonth: function (time) {
        if (time == "" || time == null || time == "null" || time == ".." || time == undefined) return "";
        var now = new Date(parseInt(time));
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        return Comm.fullZero(year) + "-" + Comm.fullZero(month);
    },
    //格式化js时间戳，格式yyyy
    formatDateYear: function (time) {
        if (time == "" || time == null || time == "null" || time == ".." || time == undefined) return "";
        var now = new Date(parseInt(time));
        var year = now.getFullYear();
        return Comm.fullZero(year);
    },
    //个位补全两位
    fullZero: function (minute) {
        return minute < 10 ? "0" + minute : minute;
    },
    //格式化字符串 超过长度用...显示
    formatStr: function (str, length) {
        if (str == undefined || str == '') {
            return;
        }
        if (length == undefined || length == '') {
            return str;
        }
        if (str.length <= length) {
            return str;
        } else {
            return str.substring(0, length - 1) + '...';
        }

    },
    //获取请求url里的get参数的值
    getParam: function (key) {
        var paramArr = location.search.substring(1);

        var arr = paramArr.split('&');
        var params = {};
        for (var i = 0; i < arr.length; i++) {
            params[arr[i].split('=')[0]] = arr[i].split('=')[1];
            if (params[key]) {
                return params[key];
            }
        }
        return null;
    },
    //元 转 万
    yuan_wan: function (str) {
        return str / 10000;
    },
    //万-元
    wan_yuan: function (str) {
        return parseInt(str * 10000);
    },
    //月 转换年
    month_year: function (str) {
        return parseInt(str / 12);
    },
    int: function (str) {
        return parseInt(str);
    }
};

//关闭弹出
function close() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

//子弹窗调用父级弹窗的方法
var ParentMethod = function (methodName, param1, param2, param3, param4) {
    try {
        switch (arguments.length) {
            case 1:
                eval("$(window.parent.document).find('#layui-layer-iframe' + (LinkUrl.Request('winname') || ''))[0].contentWindow." + methodName + "()");
                break;
            case 2:
                eval("$(window.parent.document).find('#layui-layer-iframe' + (LinkUrl.Request('winname') || ''))[0].contentWindow." + methodName + "('" + param1 + "')");
                break;
            case 3:
                eval("$(window.parent.document).find('#layui-layer-iframe' + (LinkUrl.Request('winname') || ''))[0].contentWindow." + methodName + "('" + param1 + "','" + param2 + "')");
                break;
            case 4:
                eval("$(window.parent.document).find('#layui-layer-iframe' + (LinkUrl.Request('winname') || ''))[0].contentWindow." + methodName + "('" + param1 + "','" + param2 + "','" + param3 + "')");
                break;
            case 5:
                eval("$(window.parent.document).find('#layui-layer-iframe' + (LinkUrl.Request('winname') || ''))[0].contentWindow." + methodName + "('" + param1 + "','" + param2 + "','" + param3 + "','" + param4 + "')");
                break;
            default:

        }
    } catch (e) {
        console.log(e.message);
    }
};

//子弹窗调用Iframe的方法
var Method = function (methodName, param1, param2, param3) {
    try {
        switch (arguments.length) {
            case 1:
                eval("$(window.parent.parent.document).find('#iframe')[0].contentWindow." + methodName + "()");
                break;
            case 2:
                eval("$(window.parent.parent.document).find('#iframe')[0].contentWindow." + methodName + "('" + param1 + "')");
                break;
            case 3:
                eval("$(window.parent.parent.document).find('#iframe')[0].contentWindow." + methodName + "('" + param1 + "','" + param2 + "')");
                break;
            case 4:
                eval("$(window.parent.parent.document).find('#iframe')[0].contentWindow." + methodName + "('" + param1 + "','" + param2 + "','" + param3 + "')");
                break;
            default:

        }
    }
    catch
        (e) {
        console.log(e.message);
    }
};

$(function () {
    var win_name;
    try {
        win_name = parent.layer.getFrameIndex(window.name);
    } catch (e) {
    }
    $("body").on("click", "a[data-layer],button[data-layer]", function () {
        var $obj = $(this);
        var handel = $obj.attr("data-layer-handel");
        var flog = true;
        if (handel) {
            flog = eval("flog=" + handel + "()");
        }
        if (!flog) {
            return;
        }

        var href = $obj.attr("data-layer");
        if (win_name != undefined) {
            if (href.indexOf("?") > -1) {
                href = href + "&winname=" + win_name;
            } else {
                href = href + "?winname=" + win_name;
            }
        }

        var title = $obj.attr("data-layer-title");
        var w = $obj.attr("data-layer-w");
        var h = $obj.attr("data-layer-h");
        w = w || "893px";
        h = h || "600px";
        var t = $obj.attr("data-layer-top");
        var l = $obj.attr("data-layer-left");
        if (window.screen.width < 1200) {
            t = "10px";
        }
        t = t || "";
        l = l || "";
        var shade = $obj.attr("data-layer-shade");
        shade = shade == "false" ? "0" : [0.8, '#000000'];
        var maxmin = $obj.attr("data-layer-maxmin");
        maxmin = maxmin == "true" ? true : false;
        var closeBtn = $obj.attr("data-layer-closeBtn");
        closeBtn = closeBtn == undefined ? 1 : parseInt(closeBtn);
        if (href != null && href != undefined && href != "") {
            layer_msg.open({
                type: 2,
                title: title,
                shade: false,//shade,
                shadeClose: true,//如果shade存在，shadeClose控制点击弹层外区域关闭。
                area: [w, h],
                offset: [t, l],
                content: href,
                // zIndex: layer_msg.zIndex, //重点1
                moveOut: true,
                closeBtn: closeBtn,
                maxmin: maxmin, //开启最大化最小化按钮
                moveEnd: function (e) {
                    var top = $(window.parent.document).find(e.prevObject.selector).offset().top;
                    var left = $(window.parent.document).find(e.prevObject.selector).offset().left;
                    var wh = $(window.parent.document).find(e.prevObject.selector).width();
                    if (top < 0) {
                        $(window.parent.document).find(e.prevObject.selector).css("top", 0);
                    } else if (top + 40 > parent.document.body.clientHeight) {
                        $(window.parent.document).find(e.prevObject.selector).css("top", (parent.document.body.clientHeight - 40) + "px");
                    }
                    if (left + wh < 40) {
                        $(window.parent.document).find(e.prevObject.selector).css("left", (40 - wh) + "px");
                    }
                    if (left + 40 > parent.document.body.clientWidth) {
                        $(window.parent.document).find(e.prevObject.selector).css("left", (parent.document.body.clientWidth - 40) + "px");
                    }
                }
            });
        }
    });
});


//todo 公共 js  直接控制 样式
$("body").on("blur", "input", function () {
    var val = trim($(this).val());
    $(this).val(val);
});

function trim(str) { //删除左右两端的空格
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

$(function () {
    $('input[type="checkbox"],input[type="radio"]').iCheck({
        checkboxClass: 'icheckbox_flat-blue',
        radioClass: 'iradio_flat-blue'
    });
});

$(".checkbox-inline, .radio-inline").css("padding-left", "0");

